package com.xxx.meter.model.data.swt;

import com.google.gson.Gson;
import com.xxx.meter.model.http.vo.MachineSwitchVo;
import com.xxx.meter.model.sp.SharedConst;
import com.xxx.meter.model.sp.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 设备开关工具类
 */
public class SwitchUtil {

    private static Gson gson = new Gson();

    /**
     * 获取到本地开关机时间
     */
    public static List<SwitchEntry.DataItemEntry> getMachineSwitchList() {
        List<SwitchEntry.DataItemEntry> list = pauseList();
        return list == null ? new ArrayList<SwitchEntry.DataItemEntry>() : list;
    }

    /**
     * 保存修改或者添加信息
     *
     * @param machineType 设备Type
     * @param machineId   设备Id
     * @param openTime    设备开机时间
     * @param closeTime   设备关机时间
     */
    public static void addOrUpdata(String machineType, String machineId, Long openTime, Long closeTime) {
        List<SwitchEntry.DataItemEntry> list = pauseList();
        boolean b = true;
        for (int i = 0; i < list.size(); i++) {
            SwitchEntry.DataItemEntry dataEntry = list.get(i);
            if (dataEntry.getEquipmentkey().equals(machineId) && dataEntry.getEquipmenttypekey().equals(machineType)) {
                dataEntry.setOpenTime(openTime);
                dataEntry.setCloseTime(closeTime);
                list.set(i, dataEntry);
                b = false;
                break;
            }
        }
        if (b) {
            SwitchEntry.DataItemEntry dataEntry = new SwitchEntry.DataItemEntry();
            dataEntry.setEquipmenttypekey(machineType);
            dataEntry.setEquipmentkey(machineId);
            dataEntry.setOpenTime(openTime);
            dataEntry.setCloseTime(closeTime);
            list.add(dataEntry);
        }
        saveList(list);
    }

    /**
     * 获取提交数据
     *
     * @param userName 用户名
     */
    public static String getSubmitData(String userName) {
        SwitchEntry dataEntry = pauseUser();
        if (dataEntry == null) {
            return null;
        }
        dataEntry.setUserName(userName);

        //TODO 转化格式
        MachineSwitchVo machineSubmitVo = new MachineSwitchVo();
        List<MachineSwitchVo.DataVo> submitList = new ArrayList<>();
        List<SwitchEntry.DataItemEntry> data = dataEntry.getData();
        for (int i = 0; i < data.size(); i++) {
            SwitchEntry.DataItemEntry dataItemEntry = data.get(i);
            //记录打开时间
            MachineSwitchVo.DataVo dataVoOpen = new MachineSwitchVo.DataVo();
            dataVoOpen.setEquipmentkey(dataItemEntry.getEquipmentkey());
            dataVoOpen.setEquipmenttypekey(dataItemEntry.getEquipmenttypekey());
            dataVoOpen.setTime(dataItemEntry.getOpenTime());
            dataVoOpen.setOpenType();

            //记录关闭时间
            MachineSwitchVo.DataVo dataVoClose = new MachineSwitchVo.DataVo();
            dataVoClose.setEquipmentkey(dataItemEntry.getEquipmentkey());
            dataVoClose.setEquipmenttypekey(dataItemEntry.getEquipmenttypekey());
            dataVoClose.setTime(dataItemEntry.getCloseTime());
            dataVoClose.setCloseType();

            submitList.add(dataVoOpen);
            submitList.add(dataVoClose);
        }
        machineSubmitVo.setUserName(dataEntry.getUserName());
        machineSubmitVo.setData(submitList);
        return gson.toJson(machineSubmitVo);
    }

    //是否有过巡检记录
    public static boolean isCheck() {
        return SharedPreferencesUtil.getInstance().get(SharedConst.SWITCH) != null;
    }

    //提交后清空训机
    public static void cleanMachine() {
        SharedPreferencesUtil.getInstance().save(SharedConst.SWITCH, null);
    }

    //解析用户
    private static SwitchEntry pauseUser() {
        String data = SharedPreferencesUtil.getInstance().get(SharedConst.SWITCH);
        SwitchEntry dataEntry = gson.fromJson(data, SwitchEntry.class);
        return dataEntry == null ? new SwitchEntry() : dataEntry;
    }

    //解析集合
    private static List<SwitchEntry.DataItemEntry> pauseList() {
        try {
            SwitchEntry dataEntry = pauseUser();
            List<SwitchEntry.DataItemEntry> list = dataEntry.getData();
            return list == null ? new ArrayList<SwitchEntry.DataItemEntry>() : list;
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    //保存集合
    private static void saveList(List<SwitchEntry.DataItemEntry> list) {
        SwitchEntry dataEntry = pauseUser();
        dataEntry.setData(list);
        SharedPreferencesUtil.getInstance().save(SharedConst.SWITCH, gson.toJson(dataEntry));
    }

    //保存用户
    private static void saveUser(SwitchEntry dataEntry) {
        SharedPreferencesUtil.getInstance().save(SharedConst.SWITCH, gson.toJson(dataEntry));
    }

}
